package cn.com.gridinfo.sooc.core.course.classes.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.course.classes.bean.CarourseFigure;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseDetailsInfo;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseInfo;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseKinds;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * 课程分类 dao
 * Created by jieli on 15/8/24.
 */
public class CourseDao extends SoocIDao {
    public CourseDao(SoocNetResult activity){
        super(activity);
    }

    public String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    public String statusCode;//提示信息

    private List<CourseKinds> ckList;//课程类列表
    private List<CourseInfo> courseList;//课程类列表

    private List<CarourseFigure> lunBoList;//校内轮播图对象

    private CourseDetailsInfo courseDetailsInfo;//课程详情信息对象

    public String isSuccess;//加入课程.收藏是否成功 0 失败  1成功

    public String studyId;//学习行为记录ID;


    private String courseKindsCount;//课程全部数







    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {

        if (result != null) {
            Log.d("result:["+result+"]");
            //登录获取节点服务器是没有如下两个字段的
                status = result.get("status").asText();
                statusCode = result.get("statusCode").asText();
                if (!status.equals("0")) {
                   Log.d("服务器返回错误信息: "+status+":"+statusCode);
                    return;
                }

        } else {
            return;
        }


        switch (requestCode) {
            case CodeConst.COURSE_KINDS_MENU: //课程分类
                ckList =  JsonUtil.node2pojoList(result.get("data"), CourseKinds.class);
                courseKindsCount = result.get("count").asText();
              break;
            case CodeConst.COURSE_LIST_FIRST: //课程首次请求
                courseList =  JsonUtil.node2pojoList(result.get("data"), CourseInfo.class);
                break;
            case CodeConst.COURSE_LIST_PULLUP: //课程翻页操作
                courseList =  JsonUtil.node2pojoList(result.get("data"), CourseInfo.class);
                break;
            case CodeConst.COURSE_LIST_PUSHDOWN: //课程刷新操作
                courseList =  JsonUtil.node2pojoList(result.get("data"), CourseInfo.class);
                break;
            case CodeConst.COURSE_CAROURSEFIGURE: //校内轮播图
                lunBoList = JsonUtil.node2pojoList(result.get("data"),CarourseFigure.class);
                break;
            case CodeConst.COURSE_INFO://课程详情
                Log.d("课程详情result:"+result);
                courseDetailsInfo = JsonUtil.node2pojo(result.get("data"),CourseDetailsInfo.class);
                break;
            case CodeConst.COURSE_COLLECTION://课程收藏
                isSuccess = result.get("data").get("success").asText();
                break;
            case CodeConst.COURSE_DELCOLLECTION://课程取消收藏
                isSuccess = result.get("data").get("success").asText();
                break;
            case CodeConst.COURSE_ADDMY://课程加入
                isSuccess = result.get("data").get("success").asText();
                break;

            case CodeConst.COURSE_SETBEHAVIOR_START://记录开始学习行为
                Log.d("记录开始学习行为:"+result);
                if(result.has("data")) {
                    studyId = result.get("data").get("id").asText();
                }else{
                    studyId = "0";
                }
                break;
            case CodeConst.COURSE_SETBEHAVIOR_END://记录结束学习行为
                Log.d("记录结束学习行为:"+result);
//                studyId = "";
                break;
        }

    }


    /**
     * 获取课程分类列表
     */
    public void getCourseKindsList(){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"departMenu",true);
        Log.d(url);
        RequestParams param = new RequestParams();
        param.add("uid",Arad.preferences.getString(LocalMark.USER_UID.getName()));//用户 uid
        postRequest(url,param, CodeConst.COURSE_KINDS_MENU);

    }

    /**
     * 获取课程列表
     * @param yxid 院系ID
     */
    public void getCourseListByPage(String yxid,String keyword ,int pageNo,int requestCode){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"allNewCourse",true);
        RequestParams param = new RequestParams();
        if(!StringUtils.isNull(yxid)) {
            param.add("yxid", yxid);//院系ID
        }
        if(!StringUtils.isNull(keyword)) {
            param.add("keyword", keyword);//搜索关键字
        }
        param.add("pageCount", 10+"");//每页10条
        param.add("page", pageNo+"");//第一页
        postRequest(url,param, requestCode);

    }

    /**
     * 获取校内轮播图
     */
    public void getCourseLunBo(){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sooc.toString(),"index_tj",true);
        getRequestForCode(url,new HashMap<String, String>(),CodeConst.COURSE_CAROURSEFIGURE);

    }

    /**
     * 获取课详情
     * @param kcid 课程ID
     */
    public void getCourseInfo(String kcid){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"info",true);
        RequestParams param = new RequestParams();
        param.add("kcid", kcid);//课程ID
        postRequest(url,param, CodeConst.COURSE_INFO);

    }


    /**
     * 课程收藏
     * @param kcid 课程ID
     */
    public void addCollection(String kcid){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"addCollection",true);
        RequestParams params = new RequestParams();
        params.add("kcid",kcid);
        postRequest(url,params,CodeConst.COURSE_COLLECTION);
    }

    /**
     * 课程取消收藏
     * @param kcid 课程ID
     */
    public void  delCollection(String kcid){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"deleteCollection",true);
        RequestParams params = new RequestParams();
        params.add("kcid",kcid);
        postRequest(url,params,CodeConst.COURSE_DELCOLLECTION);
    }

    /**
     * 加入课程
     * @param kcid 课程ID
     */
    public void joinCourse(String kcid){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"joinCourse",true);
        RequestParams params = new RequestParams();
        params.add("kcid",kcid);
        postRequest(url,params,CodeConst.COURSE_ADDMY);
    }

    /**
     * 记录学习行为,
     * @param kcid 课程ID
     * @param pmlid
     * @param mlid
     * @param nrid
     * @param id
     */
    public void setBehavior(String kcid,String pmlid, String mlid, String nrid ,String id,long current_time){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(),"setBehavior",true);
        RequestParams params = new RequestParams();
        params.add("kcid",kcid);
        params.add("pmlid",pmlid);
        params.add("mlid",mlid);
        params.add("nrid",nrid);
        params.add("id",id);
        params.add("current_time",current_time+"");
        if(null == id || id.equals("0")) {
            //开始学习行为
            postRequest(url, params, CodeConst.COURSE_SETBEHAVIOR_START);
        }else{
            //结束学习行为
            postRequest(url, params, CodeConst.COURSE_SETBEHAVIOR_END);

        }
    }

    public List<CourseKinds> getCkList() {
        return ckList;
    }

    public List<CourseInfo> getCourseList() {
        return courseList;
    }

    public List<CarourseFigure> getLunBoList() {
        return lunBoList;
    }

    public CourseDetailsInfo getCourseDetailsInfo() {
        return courseDetailsInfo;
    }

    public String getCourseKindsCount() {
        return courseKindsCount;
    }
}
